package com.ht.medicaltreatment.view

import android.content.Context
import android.view.View
import android.widget.FrameLayout
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ToastUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.gym.baselib.base.BaseActivity
import com.gym.baselib.utils.ProgressUtils
import com.hjq.http.EasyHttp
import com.hjq.http.listener.OnHttpListener
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.adapter.SampleLocationAdapter
import com.ht.medicaltreatment.bean.SampleLocationBean
import com.ht.medicaltreatment.bean.SampleLocationData
import com.ht.medicaltreatment.http.api.SampleLocationAPI
import kotlinx.android.synthetic.main.dialog_sample_from_layout.recyclerView

/**
 * 样本位置  选择孔位
 */
class SampleLocationDialog(context: Context, private var oldId: Int, private var newId: Int):BaseDialog(context,360f), OnItemClickListener {
    override fun getLayoutRes(): Int {
        return R.layout.dialog_sample_location_layout
    }
    private var adapter: SampleLocationAdapter
    private var onClick:(String,Int)->Unit = {name,id -> }
    init {
        adapter = SampleLocationAdapter()
        adapter.setOnItemClickListener(this)
        recyclerView.adapter = adapter
        setCanceledOnTouchOutside(true)
    }

    fun show(id:String,onClick:(String, Int)->Unit) {
        super.show()
        this.onClick = onClick
        initData(id)
    }

    private fun initData(id:String) {
        ProgressUtils.showProgress()
        EasyHttp.post(ActivityUtils.getTopActivity() as BaseActivity)
            .api(SampleLocationAPI(id))
            .request(object : OnHttpListener<SampleLocationBean> {
                override fun onHttpSuccess(result: SampleLocationBean) {
                    ProgressUtils.dismiss()
                    if(result.data != null && result.data.size > 0){
                        recyclerView.layoutManager = GridLayoutManager(context,result.data[0].sampleBoxStru.structureCol)
                        adapter.setCheckId(oldId,newId)
                        adapter.setNewInstance(result.data)
                    }else{
                        ToastUtils.showShort("没有数据")
                        dismiss()
                    }
                }

                override fun onHttpFail(e: Exception) {
                    ProgressUtils.dismiss()
                    ToastUtils.showShort(e.message)
                }
            })
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = adapter.getItem(position) as SampleLocationData
        if(item.sign == 0 && oldId != item.id){
            val text = (view as FrameLayout).getChildAt(0) as TextView
            onClick(text.text.toString(),item.id)
            dismiss()
        }else{
            ToastUtils.showShort("不可以选择这个位置")
        }
    }
}
